$(function () {
    let data = {
        pageIndex: 1,
        sId: localStorage.getItem("sId"),
        method: "dishesdata"
    };
    $("#recommonPhoto").css("display","none");
    showDataWithPage("/dishes", data);
    showDishesData();
    dishesCheckOut.checkout("#dishesForm");
});

// 列表显示
function showDishesData() {
    let reData = {
        method: "pagecount",
        sId: localStorage.getItem("sId")
    };
    $.post("/dishes", reData, function (resData) {
        let respdata = JSON.parse(resData);
        paginator.paginator("#pageLimit", respdata.count, function (page) {
            let data = {
                pageIndex: page,
                sId: localStorage.getItem("sId"),
                method: "dishesdata"
            };
            showDataWithPage("/dishes", data)
        });
    });
}

function showDataWithPage(url, data) {
    $.ajax({
        url: url,
        type: "POST",
        data: data,
        success: function (response) {
            let dishesData = JSON.parse(response);
            new myRender(dishesData, localStorage.getItem("sId")).renderTable();
        }
    })
}

function myRender(data, sId) {
    this.data = data;
    this.sId = sId;
    this.renderTable = function () {
        let tbody = $("table[class='table table-hover table-striped'] tbody");
        tbody.children().remove();
        $.each(data, function (i, val) {
            let tr = $("<tr>");
            let nameTd = $("<td>").text(val.name);
            let describeTd = $("<td>").text(val.describe);
            let img = $("<img>", {
                src: val.img,
            }).css({"width": "100px", "height": "100px"}).addClass("img-circle img-responsive");
            let imgTd = $("<td>").append(img);
            let priceTd = $("<td>").text(val.price);
            let optTd = $("<td>");
            let updateBtn = $("<button>").attr("data-toggle", "modal").attr("data-target", "#myModal")
                .text("更新").addClass("btn btn-success").css("marginRight", 10);
            let deleteBtn = $("<button>").text("删除").addClass("btn btn-danger");
            deleteListener(deleteBtn, val, sId);
            updateListener(updateBtn, val);
            optTd.append(updateBtn);
            optTd.append(deleteBtn);
            tr.append(nameTd);
            tr.append(describeTd);
            tr.append(imgTd);
            tr.append(priceTd);
            tr.append(optTd);
            tbody.append(tr);
        })
    }
}

function deleteListener(deleteBtn, item, sId) {
    deleteBtn.click("click", function () {
        $.get("/dishes?method=deletedishe&id=" + item.id + "&sId=" + sId, function (response) {
            let resp = JSON.parse(response);
            if (resp.msg == "success")
                window.location.reload();
        })
    });
}

function updateListener(updateBtn, item) {
    updateBtn.click("click", function () {
        $('#name').val(item.name);
        $('#describe').val(item.describe);
        $('#price').val(item.price);
        $('#id').val(item.id);
        $("#showImg").attr("src", item.img);
        $("#sId").val(localStorage.getItem("sId"));
    });
}

// 更新
$('.confirmData').on("click", function () {
    $("#dishesForm").data("bootstrapValidator").validate();
    if (!$("#dishesForm").data("bootstrapValidator").isValid()) {
        return false;
    }
    let formData = new FormData($("#dishesForm")[0]);
    $.ajax({
        url: "/dishes",
        method: "POST",
        data: formData,
        cache: false,
        processData: false,
        contentType: false,
        success: (res) => {
            let resp = JSON.parse(res);
            if (resp.msg == "success") {
                $(".msgSuc").show();
                setTimeout(() => {
                    $(".msgSuc").hide();
                }, 2000);
                window.location.reload();
            } else {
                $(".msgErr").show();
                setTimeout(() => {
                    $(".msgErr").hide();
                }, 2000)
            }
        }
    })
});

// 查询
$(".confirmFind").on("click", function () {
    let name = $("#findName").val();
    if (name != "") {
        showDataWithPage("/dishes", {
            pageIndex: 1,
            name: name,
            sId: localStorage.getItem("sId"),
            method: "loadDisheWithPage"
        });

        let data = {
            name: name,
            method: "loadDisheCount",
            sId: localStorage.getItem("sId")
        };
        $.post("/dishes", data, function (response) {
            $("#findModal").modal('hide');
            let dishesCount = JSON.parse(response);
            paginator.paginator("#pageLimit", dishesCount.count, function (page) {
                let data1 = {
                    pageIndex: page,
                    name: name,
                    sId: localStorage.getItem("sId"),
                    method: "loadDisheWithPage"
                };
                showDataWithPage("/dishes", data1)
            });
        })
    } else {
        $("#findModal").modal('hide');
        window.location.reload();
    }
});

// 自荐
$(".confirmComon").on("click", function () {
    $("#mysId").val(localStorage.getItem("sId"));
    let formData = new FormData($("#recommonForm")[0]);
    $.ajax({
        url: "/store",
        method: "POST",
        data: formData,
        cache: false,
        processData: false,
        contentType: false,
        success: (res) => {
            let resp = JSON.parse(res);
            if (resp.msg == "success") {
                $("#commonModal").modal('hide');
                $(".pushMsgSucc").show();
                setTimeout(() => {
                    $(".pushMsgSucc").hide();
                }, 2000);

            } else {
                $(".pushMsgErr").show();
                setTimeout(() => {
                    $(".pushMsgErr").hide();
                }, 2000)
            }
        }
    })
});